<?php


if (isset($_GET['room']) &&
    isset($_GET['user']) &&
    isset($_GET['pass']) )
{
  include_once 'init.php';
  $modified = unlog_users();

  if ($chat_data['kick'][$_GET['user']] ||
      $chat_data['kick'][$_SERVER['REMOTE_ADDR']])
  {
    echo "FAILED\r\n$chat_err_kick";
    die;
  }

  $modified = true;

  $room = $_GET['room'];
  $user = $_GET['user'];
  $pass = $_GET['pass'];

  if (!preg_match('/^\w+( +\w+)*$/', $room))        { echo "FAILED\r\n$chat_err_inval"; die; }
  if (!preg_match('/^\w+$/',         $user))        { echo "FAILED\r\n$chat_err_inval"; die; }
  if (!chat_chk($user,   $pass))        { echo "FAILED\r\n$chat_err_inval"; die; }
  if (isset($chat_data['user'][$user])) { $GLOBALS['chat_data']['data'][] = "-\r\n{$chat_data['room'][$user]}\r\n{$user}"; }

  $chat_data['room'][$user] = $room;
  $chat_data['user'][$user] = time();
  $chat_data['pass'][$user] = md5(rand());
  $chat_data['data'][] = "+\r\n{$room}\r\n{$user}";
  echo "OK\r\n{$user}\r\n{$chat_data['pass'][$user]}\r\n";

  if ($modified) file_put_contents('data.txt', serialize($chat_data));
}

?>